package com.itheima.controller;

import com.itheima.aop.annotation.LogOperater;
import com.itheima.aop.annotation.MyLog;
import com.itheima.pojo.Clazz;
import com.itheima.pojo.ClazzQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.ClazzService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author：cxl
 * @Package：com.itheima.controller
 * @Project：web-project
 * @name：ClazzController
 * @Date：2025-09-25 2:01 pm
 * @Filename：ClazzController
 */
@Slf4j
@RestController
@RequestMapping("/clazzs")
public class ClazzController {
    @Autowired
    ClazzService clazzService;
    //回显，根据id查询班级

    
    @GetMapping("/{id}")
    public Result selectById(@PathVariable Integer id){
        Clazz clazz=clazzService.selectById(id);
        return Result.success(clazz);

    }

    //修改班级
    @LogOperater
    @PutMapping
      public Result update(@RequestBody Clazz clazz){
        Clazz clazzNew=clazzService.update(clazz);
        return Result.success(clazz);
      }


    //删除班级
    @LogOperater
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        clazzService.deleteById(id);
        return Result.success();
    }

    //班级的分页条件查询
    @LogOperater
    @GetMapping
    public Result selectByCondition(ClazzQueryParam clazzQueryParam) {
        PageResult<Clazz> pageResult = clazzService.page(clazzQueryParam);
        log.info("班级的分页查询{}", pageResult);
        return Result.success(pageResult);
    }

    //查询所有班级
    @LogOperater
    @GetMapping("/list")
    public Result selectAll() {
        List<Clazz> clazzList = clazzService.selectAll();
        log.info("在查所有班级");
        return Result.success(clazzList);
    }

    //添加班级
    @LogOperater
    @PostMapping
    public Result add(@RequestBody Clazz clazz) {
        log.info("添加班级clazz=" + clazz);
        clazzService.add(clazz);
        return Result.success();
    }
}
